﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;

namespace Gmantis.Controls
{
    public class TabItemAutomationPeer : ItemAutomationPeer, ISelectionItemProvider
    {
        // Methods
        public TabItemAutomationPeer(object owner)
            : base(owner as UIElement)
        {
        }

        protected override AutomationControlType GetAutomationControlTypeCore()
        {
            return AutomationControlType.TabItem;
        }

        protected override List<AutomationPeer> GetChildrenCore()
        {
            List<AutomationPeer> childrenCore = base.GetChildrenCore();
            TabItem item = base.Item as TabItem;
            if ((item != null) && item.IsSelected)
            {
                TabControl tabOwner = this.TabOwner;
                if (tabOwner == null)
                {
                    return childrenCore;
                }
                Panel itemsHost = tabOwner.GetItemsHost();
                if (itemsHost == null)
                {
                    return childrenCore;
                }
                List<AutomationPeer> children = new FrameworkElementAutomationPeer(itemsHost).GetChildren();
                if (children == null)
                {
                    return childrenCore;
                }
                if (childrenCore == null)
                {
                    return children;
                }
                childrenCore.AddRange(children);
            }
            return childrenCore;
        }

        protected override string GetClassNameCore()
        {
            return base.Owner.GetType().Name;
        }

        protected override string GetNameCore()
        {
            TabItem owner = base.Owner as TabItem;
            return ((owner.Header as string) ?? string.Empty);
        }

        public override object GetPattern(PatternInterface patternInterface)
        {
            if (patternInterface == PatternInterface.SelectionItem)
            {
                return this;
            }
            return base.GetPattern(patternInterface);
        }

        protected override bool IsKeyboardFocusableCore()
        {
            TabControl tabOwner = this.TabOwner;
            if ((tabOwner == null) || !tabOwner.IsEnabled)
            {
                return false;
            }
            TabItem owner = base.Owner as TabItem;
            return (owner.IsEnabled && owner.IsTabStop);
        }

        internal void RaiseAutomationIsSelectedChanged(bool isSelected)
        {
            base.RaisePropertyChangedEvent(SelectionItemPatternIdentifiers.IsSelectedProperty, !isSelected, isSelected);
        }

        void ISelectionItemProvider.AddToSelection()
        {
            if (!base.IsEnabled())
            {
                throw new ElementNotEnabledException();
            }
            TabControl tabOwner = this.TabOwner;
            if (tabOwner != null)
            {
                tabOwner.SelectedItem = base.Item;
            }
        }

        void ISelectionItemProvider.RemoveFromSelection()
        {
        }

        void ISelectionItemProvider.Select()
        {
            if (!base.IsEnabled())
            {
                throw new ElementNotEnabledException();
            }
            TabControl tabOwner = this.TabOwner;
            if (tabOwner != null)
            {
                tabOwner.SelectedItem = base.Item;
            }
        }

        // Properties
        bool ISelectionItemProvider.IsSelected
        {
            get
            {
                TabControl tabOwner = this.TabOwner;
                return ((tabOwner != null) && base.Item.Equals(tabOwner.SelectedItem));
            }
        }

        IRawElementProviderSimple ISelectionItemProvider.SelectionContainer
        {
            get
            {
                return base.ProviderFromPeer(base.ItemsControlAutomationPeer);
            }
        }

        private TabControl TabOwner
        {
            get
            {
                if (base.ItemsControlAutomationPeer != null)
                {
                    return (base.ItemsControlAutomationPeer.Owner as TabControl);
                }
                return null;
            }
        }
    }
}
